#!/usr/bin/env python3
"""
筛选GitHub和Copilot相关请求的脚本
"""

import re
import os
from datetime import datetime

def filter_github_copilot_requests(input_file, output_file):
    """筛选GitHub和Copilot相关的HTTP请求"""
    
    # GitHub和Copilot相关的域名和关键词
    target_keywords = [
        'github.com',
        'api.github.com', 
        'avatars.githubusercontent.com',
        'copilot',
        'githubcopilot.com',
        'copilot-telemetry',
        'telemetry.individual.githubcopilot.com',
        'api.individual.githubcopilot.com',
        'proxy.individual.githubcopilot.com',
        'copilot-proxy.githubusercontent.com'
    ]
    
    try:
        with open(input_file, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # 按交互分割
        interactions = re.split(r'={80}\n交互 #\d+', content)
        
        # 保留匹配的交互
        filtered_interactions = []
        match_count = 0
        
        for i, interaction in enumerate(interactions):
            if i == 0:  # 跳过文件头部
                continue
                
            # 检查是否包含目标关键词
            is_match = False
            for keyword in target_keywords:
                if keyword.lower() in interaction.lower():
                    is_match = True
                    break
            
            if is_match:
                match_count += 1
                # 重新添加分割符
                filtered_interactions.append(f"={'=' * 80}\n交互 #{match_count}" + interaction)
        
        # 写入新文件
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write("=" * 80 + "\n")
            f.write("GitHub & Copilot 相关请求筛选结果\n")
            f.write(f"原始文件: {input_file}\n")
            f.write(f"筛选时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
            f.write(f"匹配的交互数量: {match_count}\n")
            f.write(f"输出路径: {os.path.abspath(output_file)}\n")
            f.write("=" * 80 + "\n\n")
            
            # 写入匹配的内容
            for interaction in filtered_interactions:
                f.write(interaction)
                f.write("\n")
        
        print(f"✅ 筛选完成！")
        print(f"📊 找到 {match_count} 个GitHub/Copilot相关的HTTP交互")
        print(f"📁 输出文件: {os.path.abspath(output_file)}")
        
        return match_count
        
    except Exception as e:
        print(f"❌ 筛选失败: {e}")
        return 0

def main():
    """主函数"""
    # 输入和输出文件路径
    input_file = "/home/hxz/study/mitmproxy_traffic_export.txt"
    
    # 检查输入文件是否存在
    if not os.path.exists(input_file):
        print(f"❌ 输入文件不存在: {input_file}")
        return
    
    # 确定输出文件路径（当前目录）
    output_file = "github_copilot_requests.txt"
    
    print(f"🔍 开始筛选GitHub和Copilot相关请求...")
    print(f"📄 输入文件: {input_file}")
    print(f"📁 输出文件: {output_file}")
    print("-" * 60)
    
    # 执行筛选
    match_count = filter_github_copilot_requests(input_file, output_file)
    
    if match_count > 0:
        print(f"\n🎉 筛选成功！共找到 {match_count} 个相关请求")
        print(f"🔗 包含的主要服务:")
        print("   • GitHub API (api.github.com)")
        print("   • GitHub Copilot API (api.individual.githubcopilot.com)")
        print("   • Copilot遥测服务 (telemetry.individual.githubcopilot.com)")
        print("   • GitHub头像服务 (avatars.githubusercontent.com)")
        print("   • 其他GitHub相关服务")
    else:
        print("\n❌ 未找到GitHub或Copilot相关的请求")

if __name__ == "__main__":
    main()